﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArduinoSketch.Core
{
    public class Library
    {
        private string _libraryName;

        public string LibraryName
        {
            get { return _libraryName; }
            set { _libraryName = value; }
        }

        private List<string> _libraryNames;

        public List<string> LibraryNames
        {
            get { return _libraryNames; }
            set { _libraryNames = value; }
        }
        private string _fullPath;

        public string FullPath
        {
            get { return _fullPath; }
            set { _fullPath = value; }
        }
        private List<string> _keyWord1;

        public List<string> KeyWord1
        {
            get { return _keyWord1; }
            set { _keyWord1 = value; }
        }
        private List<string> _keyWord2;

        public List<string> KeyWord2
        {
            get { return _keyWord2; }
            set { _keyWord2 = value; }
        }
        private List<string> _literal1;

        public List<string> Literal1
        {
            get { return _literal1; }
            set { _literal1 = value; }
        }
        private List<string> _libraryFile;

        public List<string> LibraryFile
        {
            get { return _libraryFile; }
            set { _libraryFile = value; }
        }

        public Library()
        {
            KeyWord1 = new List<string>();
            KeyWord2 = new List<string>();
            Literal1 = new List<string>();
            LibraryFile = new List<string>();
            LibraryNames = new List<string>();
        }
    }

    public class LibraryFactory
    {

        private string ardu = ""; 

        private Dictionary<string, Library> _library;

        public LibraryFactory()
        {
            _library = new Dictionary<string, Library>();
        }

        public void BuildLibrary()
        {
            this.DoBuildLibrary();
        }

        public Dictionary<string, Library> Library
        {
            get { return _library; }
        }

        private void DoBuildLibrary()
        {
            try
            {
                ArduinoTargetConfig _arduinoTarget = Utils.LoadArduinoTargetConfig();
                ardu = _arduinoTarget.ArduinoLibDirectory; 

                DirectoryInfo _dirInfo = new DirectoryInfo(ardu);
                DirectoryInfo[] _dirInfos = _dirInfo.GetDirectories();
                _library.Clear();
                foreach (var _dir in _dirInfos)
                {
                    Library _lib = new Library();
                    _lib.LibraryName = _dir.Name;
                    _lib.FullPath = _dir.FullName;
                    _library.Add(_dir.Name, _lib);
                    FileInfo[] _fileInfos = _dir.GetFiles();
                    foreach (var _files in _fileInfos)
                    {
                        if (_files.Name.Equals("keywords.txt"))
                        {
                            LoadKeyWord(_files, _lib);
                        }
                        else if ((_files.Extension.Equals(".cpp")) || (_files.Extension.Equals(".c")))
                        {
                            LoadLibraryFile(_files, _lib);
                        }
                        else if ((_files.Extension.Equals(".h")))
                        {
                            _lib.LibraryNames.Add(_files.Name);
                        }
                    }
                }
            }
            catch
            {
                ArduioSetting _setting = new ArduioSetting();
                if (_setting.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    DoBuildLibrary(); 
            }
           
        }

        private void LoadKeyWord(FileInfo fileInfo, Library lib)
        {
            using (Stream fsr = new FileStream(fileInfo.FullName, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fsr))
                {
                    string _strLine;
                    while ((_strLine = sr.ReadLine()) != null)
                    {
                        if (_strLine.IndexOf('#') == -1)
                        {
                            string[] _str = _strLine.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            if (_str.Length == 2)
                            {
                                switch (_str[1].ToUpper())
                                {
                                    case "LITERAL1": lib.Literal1.Add(_str[0]);
                                        break;
                                    case "KEYWORD1": lib.KeyWord1.Add(_str[0]);
                                        break;
                                    case "KEYWORD2": lib.KeyWord2.Add(_str[0]);
                                        break;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void LoadLibraryFile(FileInfo fileInfo, Library lib)
        {

        }
    }
}
